package com.sgb.kjwl.view.ui.adapter.self;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AdapterSelfFirstordernestingrecyclerviewItemBinding;
import com.sgb.kjwl.model.entity.businessmanagement.CompanyEntity;
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity;
import com.sgb.kjwl.utils.MineAdapterItemClick;
import com.swgk.core.FlowLogUtils;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;


/*
  个人中心的权限菜单的第一层RecycleView适配器
 */
public class SelfFirstOrderNestingAdapter extends BaseQuickAdapter<PermissionMenuEntity.ListEntity, BaseViewHolder> {
    //个人中心的权限菜单的第二层RecycleViewView控件
    private RecyclerView list_publish;
    private List<CompanyEntity> mCompanyList;
    private int mType;
    private FlowLogUtils flowLogUtils;
    private Context context;
    private AdapterSelfFirstordernestingrecyclerviewItemBinding item;
    private TextView tvTitle;
    private ArrayList<PermissionMenuEntity.ListEntity> mHomeList = new ArrayList<>();//首页菜单全部

    private ArrayList<CompanyEntity> mCompanysList = new ArrayList<>();//公司列表

    private ClickMorelistener clickMorelistener;

    private ArrayList<PermissionMenuEntity.ListEntity> mListTemp = new ArrayList<>();//临时


    // 绑定数据和参数
    public SelfFirstOrderNestingAdapter(Context context, List<PermissionMenuEntity.ListEntity> datas, List<CompanyEntity> companyList, int type) {
        super(R.layout.adapter_self_firstordernestingrecyclerview_item, datas);
        this.mCompanyList = companyList;
        mType = type;
        this.context = context;
        getReallyData(datas);
    }

    public void setListData(List<PermissionMenuEntity.ListEntity> datas, List<CompanyEntity> companyList, int type) {
        this.mCompanyList = companyList;
        this.mType = type;
        getReallyData(datas);
    }

    public void setDatas(List<PermissionMenuEntity.ListEntity> datas) {
        getReallyData(datas);
    }

    private void getReallyData(List<PermissionMenuEntity.ListEntity> list) {
        mHomeList.clear();
        mListTemp.clear();
        if (list != null && list.size() == 1 && "首页".equals(list.get(0).getTitle())) {
            PermissionMenuEntity.ListEntity second = list.get(0);
            PermissionMenuEntity.ListEntity secondReally = new PermissionMenuEntity.ListEntity();
            if (second != null && second.getList() != null) {
                mHomeList.addAll(second.getList());
                if (second.getList().size() > 16) {
                    mListTemp.addAll(second.getList().subList(0, 15));
                } else {
                    mListTemp.addAll(second.getList());
                }
                PermissionMenuEntity.ListEntity moreEntity = new PermissionMenuEntity.ListEntity();
                moreEntity.setIcon("more");
                moreEntity.setTitle("更多");
                moreEntity.setName("0_gengduo");
                mListTemp.add(moreEntity);
                secondReally.setList(mListTemp);
                list.clear();
                list.add(secondReally);
            }
        }
        setNewData(list);
    }


    //实现具体操作
    @Override
    public void convert(BaseViewHolder holder, PermissionMenuEntity.ListEntity entity) {
        item = DataBindingUtil.bind(holder.itemView);
        holder.itemView.setVisibility(View.VISIBLE);
        //DataBinding绑定布局，实现数据传递
        item.setViewModel(entity);
        //找到第二层RecycleView控件
        list_publish = holder.itemView.findViewById(R.id.list_publish);
        tvTitle = holder.itemView.findViewById(R.id.thesecondfloor_title);
        tvTitle.setVisibility(TextUtils.isEmpty(entity.getTitle()) || "首页".equals(entity.getTitle()) ? View.GONE : View.VISIBLE);
        //设置布局管理器
        list_publish.setLayoutManager(new GridLayoutManager(context, 4));
        //获取数据集合
        //DataBinding绑定布局，实现数据传递
        //holder.setBinding(BR.viewModel, entity);
        //找到第二层RecycleView控件
        //list_publish = holder.itemView.findViewById(R.id.list_publish);
        //设置布局管理器
        //list_publish.setLayoutManager(new GridLayoutManager(context, 4));
        //获取数据集合
        List<PermissionMenuEntity.ListEntity> list = entity.getList();
        //新建第二层RecycleView适配器对象并传参
        SelfSecondOrderNestingAdapter selfSecondOrderNestingAdapter = new SelfSecondOrderNestingAdapter(context, list);
        selfSecondOrderNestingAdapter.setOnItemClick(new AdapterOnItemClick<PermissionMenuEntity.ListEntity>() {
            @Override
            public void onItemClick(PermissionMenuEntity.ListEntity listEntity, int position) {
                if (listEntity.getName().equals("0_gengduo")){
                    clickMorelistener.onClickMorelistener();
                }else {
                    MineAdapterItemClick.getInstance().adapterItemClick(listEntity, context);
                }
                useModeNumToService(entity.getTitle(), listEntity.getName());

            }
        });
        list_publish.setAdapter(selfSecondOrderNestingAdapter);
        holder.itemView.findViewById(R.id.head).setVisibility(holder.getLayoutPosition() == 0 ? View.VISIBLE : View.GONE);
        //第二层RecycleView控件设置适配器
    }

    //用户点击事件数据上报
    private void useModeNumToService(String tabName, String titleName) {
        if (flowLogUtils == null) {
            flowLogUtils = new FlowLogUtils();
        }
        flowLogUtils.useMySelfModeNumToService(tabName, titleName);
    }

    public void setOnClickMorelistener(ClickMorelistener clickMorelistener){
        this.clickMorelistener= clickMorelistener;
    }
    public interface ClickMorelistener{
        void onClickMorelistener();
    }
}
